//
//  SmallDecisionsBaseTabBarController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/6/14.
//

#import "SmallDecisionsBaseTabBarController.h"
#import "SmallDecisionsHomeViewController.h"
#import "SmallDecisionsMyViewController.h"
#import "SmallDecisionsBaseNavigationController.h"

@interface SmallDecisionsBaseTabBarController ()<UITabBarControllerDelegate>

@end

@implementation SmallDecisionsBaseTabBarController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [SmallDecisionsNavigatorManager shareInstance].mainNavigationController = (UINavigationController *)self.viewControllers[self.selectedIndex];;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTabBarSubViewControllers];
}

- (void)addTabBarSubViewControllers {
    if (@available(iOS 15.0, *)) {
        UITabBarAppearance *bar = [UITabBarAppearance new];
        bar.backgroundColor = SmallDecisionsTabBgColor;
        bar.backgroundEffect = nil;
        bar.stackedLayoutAppearance.selected.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:SmallDecisionsThemeColor};
        bar.stackedLayoutAppearance.normal.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"0xD8D8D8"]};
        self.tabBar.scrollEdgeAppearance = bar;
        self.tabBar.standardAppearance = bar;
    } else {
        self.tabBar.backgroundColor = SmallDecisionsTabBgColor;
        self.tabBar.barTintColor = SmallDecisionsThemeColor;
        self.tabBar.translucent = NO;
        [[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:SmallDecisionsTabBgColor]];
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:SmallDecisionsThemeColor} forState:UIControlStateSelected];

    }
   
    self.tabBar.translucent = NO;
    self.delegate = self;
    
    SmallDecisionsHomeViewController * homeVC = [[SmallDecisionsHomeViewController alloc] init];
    SmallDecisionsBaseNavigationController *homeNavVC = [[SmallDecisionsBaseNavigationController alloc] initWithRootViewController:homeVC];
    homeNavVC.navigationBar.translucent = NO;
    UITabBarItem *homeTabItem = [self setTabBarItemWithTitle:@"首页" imageName:@"tab_home_n" selectedImageName:@"tab_home_h"];
    homeVC.tabBarItem = homeTabItem;
    
    SmallDecisionsMyViewController * myVC = [[SmallDecisionsMyViewController alloc] init];
    SmallDecisionsBaseNavigationController * myNavVC = [[SmallDecisionsBaseNavigationController alloc] initWithRootViewController:myVC];
    myNavVC.navigationBar.translucent = NO;
    UITabBarItem *myTabItem = [self setTabBarItemWithTitle:@"设置" imageName:@"tab_my_n" selectedImageName:@"tab_my_h"];
    myNavVC.tabBarItem = myTabItem;

    NSArray *viewControllersArray = [NSArray arrayWithObjects:homeNavVC, myNavVC, nil];
    self.viewControllers = viewControllersArray;
}

- (UITabBarItem *)setTabBarItemWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    
    UITabBarItem *tabItem = [[UITabBarItem alloc] initWithTitle:title image:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    return tabItem;
}

- (void)tabBarController:(UITabBarController *)tabBarController
 didSelectViewController:(UIViewController *)viewController {
    [SmallDecisionsNavigatorManager shareInstance].mainNavigationController = (UINavigationController *)viewController;
    self.selectedIndex = tabBarController.selectedIndex;
}

- (BOOL)shouldAutorotate{
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask) supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
@end
